// ***************************************************************************
// Caution: modifying this file will affect the integrity of the application.
// ***************************************************************************
// Copyright ?1995-2007, Camstar Systems, Inc. All Rights Reserved.
// This file is used to toggle the languages for the specified window and
// any frames within the window, traversing through to the last frame

// Variable used to determine if the default or local language should be used.
// When the user first logs in they will be using the default so this variable needs to be true .
var mGetLocal = true;

// Toggle languages call from the icon
function ToggleLanguages(docWindow)
{
	// Make a call to toggle the languages
	PerformToggleLanguages(docWindow);
	// Set the value for local or default
	var iFrameSource = 	top.PageMainHiddenIframe;
	iFrameSource += '?' + top.QueryStringUseDefaultLanguage + '=' + mGetLocal;
	gDoc[top.CtrlMainHiddenIframe].src = iFrameSource;		
	mGetLocal = !mGetLocal;	
} // ToggleLanguages

// Set current language
// Invoked after updating to set proper language
function SetLocalization(docWindow, getLocalLabels)
{
	var currentGetLocal = mGetLocal;
	mGetLocal = ! getLocalLabels;
	
	// We don't do actual toggling - just refreshing the labels
	PerformToggleLanguages(docWindow);

	// Restore the value
	mGetLocal = currentGetLocal;	
} // SetLocalization

// Toggle languages for a given window and in turn
// call the function again if there are child windows 
function PerformToggleLanguages(docWindow)
{
	// Switch the labels for the window
	SwitchLabels(docWindow.document, mGetLocal);
	
	// Get the frame count for the window
	var frameCount = docWindow.frames.length;
	
	// Loop through each frame from the main window
	for(var i=0; i < frameCount; i++) 
	{ 
		// Do we need to loop through the children frames
		if(docWindow.frames[i].window.frames.length != 0)
		{
			PerformToggleLanguages(docWindow.frames[i].window);
		} 
		else
		{
			SwitchLabels(docWindow.frames[i].window.document, mGetLocal);
		}// if 
	} // for
} // PerformToggleLanguages 

// Switch the labels for the given document
function SwitchLabels(doc, getLocal)
{
	var scriptWindow = doc.parentWindow;
		
	// For the various types of form elements, switch the labels
	var elements = new Array(top.TagSpan.toLowerCase(), top.TagImg.toLowerCase(), 
	        top.TagA.toLowerCase(), top.TagButton.toLowerCase(),top.TagInput.toLowerCase() , "div"); 
	
	// Save pointer to function to speed up 
	var getLabelFunction = GetLabel;
	for(var i=0; i < elements.length; i++) 
	{ 
	    if (i!=5)
	    {
		    SwitchElementLabels(doc, doc.getElementsByTagName(elements[i]), getLocal, getLabelFunction);				
	    }
	    else
	    {
	        // Switch language text for tooltips
		    SwitchTooltipLabels(doc.getElementsByTagName(elements[i]), getLocal);				
		}
	} // for			
    
	// Toggle header labels for grids
	if (scriptWindow)
	{
	    if (scriptWindow.name)
	    {
	        var windowName = scriptWindow.name.substr(0, 13);
	        // Skip SmartNavigation window
	        if (windowName == '__hifSmartNav')
	            scriptWindow = scriptWindow.parent;
        }	        
    }	
    scriptWindow.execScript('try { ToggleLanguageForGrids(' + getLocal + '); } catch(e) { }', 'JavaScript');
} // SwitchLabels

// Get the label for the specified element
function GetLabel(doc, element, getLocal)
{
	var labelText = null;
	
	// Is this a valid element
	if(element)
	{
		if(getLocal)
		{
			// Is the default label avialable
			if(element.defaultlabel != null)
			{
				labelText = element.defaultlabel;
			} // if
		}
		else
		{
			// Is the local label avialable
			if(element.locallabel != null)
			{
				labelText = element.locallabel;
			} // if
		} // if else
	} // if
	
	return labelText; 

} // GetLabel

// Switch the elements
function SwitchElementLabels(doc, labelElements, getLocal, getLabelFunction)
{
	for(var i=0; i < labelElements.length; i++) 
	{ 
		// Get the text
		var targetElement = doc.all[labelElements[i].id];
		var elementText = getLabelFunction(doc, targetElement, getLocal);

		// Is there a valid label
		if(elementText)
		{
			// Set the correct label
			if(targetElement.tagName == top.TagSpan.toUpperCase())
			{
				targetElement.innerHTML = elementText;
			}
			else if(targetElement.tagName == top.TagImg.toUpperCase())
			{
				targetElement.title = elementText;
			}
			else if (targetElement.tagName == top.TagA.toUpperCase())
			{
				targetElement.innerText = elementText;
			}
			else if (targetElement.tagName == top.TagInput.toUpperCase())
			{
				targetElement.value = elementText;
				targetElement.title = elementText;
			}
			else if(targetElement.tagName == top.TagButton.toUpperCase())
			{
				targetElement.value = elementText;
			}
		} // if
	} // for		
} // SwitchElementLabels

// Switch the elements in tooltips
function SwitchTooltipLabels(labelElements, getLocal)
{
	for(var i=0; i < labelElements.length; i++) 
	{ 
		// Get the text
		var targetElement = labelElements[i];
		var defaultLabel = targetElement.getAttribute("defaultLabel");
		if (defaultLabel)
        {		
	        if(getLocal)
	        {
			    targetElement.title = defaultLabel;
	        } 
	        else
	        {
		        // Is the default label avialable
		        var localLabel = targetElement.getAttribute("localLabel");
		        if (localLabel)
			        targetElement.title = localLabel;
			    else
			        targetElement.title = defaultLabel;
	        }
		}
	} // for		
} // SwitchTooltipLabels
